package com.coldchain.cys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.cys.entity.CarAccident;
import com.coldchain.cys.mapper.CarAccidentMapper;
import com.coldchain.cys.page.CarAccidentQueryVo;
import com.coldchain.cys.service.ICarAccidentService;
import com.coldchain.cys.service.ICarDriverService;
import com.coldchain.cys.service.ICarService;
import com.fangliai.core.context.BaseContextHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianpeng
 * @since 2018-09-20
 */
@Service
public class CarAccidentServiceImpl extends ServiceImpl<CarAccidentMapper, CarAccident> implements ICarAccidentService {

	@Autowired
	private ICarDriverService carDriverService ;
	
	@Autowired
	private ICarService carService ;
	
	@Transactional
	@Override
	public BaseResponse doAddCarAccident(CarAccident carAccident) {

		if(StrUtil.isBlank(carAccident.getType())) {
			return getErrorMessage("必须选择事故类型") ;
		}
		if(StrUtil.isBlank(carAccident.getLon())) {
			return getErrorMessage("必须所在位置") ;
		}
		if(StrUtil.isBlank(carAccident.getLat())) {
			return getErrorMessage("必须所在位置") ;
		}
		carAccident.setDriverid(BaseContextHandler.getUserID());
		super.save(carAccident) ;
		return new ObjectRestResponse<>().succeed("上报成功") ;
	}
	
	public BaseResponse getErrorMessage(String message) {
		return new ObjectRestResponse<>().error(message) ;
	}

	@Override
	public void getCarAccidentList(CarAccidentQueryVo vo) {
		QueryWrapper<CarAccident> ew = new QueryWrapper<CarAccident>();
		ew.orderByDesc("createtime") ;
		if(vo.getDriverid() != null) {
			ew.eq("driverid", vo.getDriverid()) ;
		}
		super.page(vo, ew) ;
	}
}
